<template>
  <div>
    <template>
      <div class="scekill_activity_container">
        <div class="top_box">
          <a-card style="width: 100%">
            <div>
              <div class="filter_search">
                <div>
                  <a-icon type="search" />
                  筛选搜索
                </div>
                <div class="btnbox">
                  <a-button class="btnLeft"> 重置 </a-button>
                  <a-button type="primary"> 查询搜索 </a-button>
                </div>
              </div>
              <div class="activity_box">
                活动名称：<a-input
                  placeholder="活动名称"
                  style="width: 200px"
                />
              </div>
            </div>
          </a-card>
        </div>
        <div class="data_box">
          <a-card style="width: 100%">
            <div class="data_list">
              <div>
                <a-icon type="container" />
                数据列表
              </div>
              <div class="btnbox">
                <a-button size="small" class="btnLeft">
                  秒杀时间段列表
                </a-button>
                <a-button size="small"> 参加活动 </a-button>
              </div>
            </div>
          </a-card>
        </div>
        <div class="data_table">
          <a-table
            :columns="columns"
            :data-source="data"
            bordered
            :row-selection="rowSelection"
          >
            <span slot="num"> {{ num }} </span>
            <span slot="activitytitle">春季家电家具疯狂秒杀</span>
            <span slot="state">活动未开始</span>
            <span
              slot="online"
            ><a-switch
              default-checked
              @change="onChange"
            /></span>
            <span slot="action">
              <a-button type="link"> 设置商品 </a-button>
              <a-button type="link"> 编辑 </a-button>
              <a-button type="link"> 删除 </a-button>
            </span>
          </a-table>
        </div>
      </div>
    </template>
  </div>
</template>

<script>
const columns = [
  {
    title: '编号',
    dataIndex: 'num',
    width: '10%',
    scopedSlots: { customRender: 'num' },
    align: 'center'
  },
  {
    title: '活动标题',
    dataIndex: 'activitytitle',
    width: '25%',
    scopedSlots: { customRender: 'activitytitle' },
    align: 'center'
  },
  {
    title: '活动状态',
    dataIndex: 'state',
    width: '10%',
    scopedSlots: { customRender: 'state' },
    align: 'center'
  },
  {
    title: '结束时间',
    dataIndex: 'endTime',
    scopedSlots: { customRender: 'endTime' },
    align: 'center'
  },
  {
    title: '上线/下线',
    dataIndex: 'online',
    scopedSlots: { customRender: 'online' },
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '20%',
    scopedSlots: { customRender: 'action' },
    align: 'center'
  }
]

const data = []
for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`
  })
}
export default {
  data() {
    this.cacheData = data.map((item) => ({ ...item }))
    return {
      data,
      columns
    }
  },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(
            `selectedRowKeys: ${selectedRowKeys}`,
            'selectedRows: ',
            selectedRows
          )
        },
        getCheckboxProps: (record) => ({
          props: {
            disabled: record.name === 'Disabled User', // Column configuration not to be checked
            name: record.name
          }
        })
      }
    }
  },
  methods: {
    handleChange(value, key, column) {
      const newData = [...this.data]
      const target = newData.find((item) => key === item.key)
      if (target) {
        target[column] = value
        this.data = newData
      }
    },
    edit(key) {
      const newData = [...this.data]
      const target = newData.find((item) => key === item.key)
      this.editingKey = key
      if (target) {
        target.editable = true
        this.data = newData
      }
    },
    save(key) {
      const newData = [...this.data]
      const newCacheData = [...this.cacheData]
      const target = newData.find((item) => key === item.key)
      const targetCache = newCacheData.find((item) => key === item.key)
      if (target && targetCache) {
        delete target.editable
        this.data = newData
        Object.assign(targetCache, target)
        this.cacheData = newCacheData
      }
      this.editingKey = ''
    },
    cancel(key) {
      const newData = [...this.data]
      const target = newData.find((item) => key === item.key)
      this.editingKey = ''
      if (target) {
        Object.assign(
          target,
          this.cacheData.find((item) => key === item.key)
        )
        delete target.editable
        this.data = newData
      }
    },
    onChange(checked) {
      console.log(`a-switch to ${checked}`)
    }
  }
}
</script>

<style lang="scss">
.scekill_activity_container {
  margin: 20px;

  .top_box,
  .data_box {
    margin-bottom: 20px;
  }

  .filter_search {
    display: flex;
    justify-content: space-between;
  }
  .btnLeft {
    margin-right: 10px;
  }

  .data_list {
    display: flex;
    justify-content: space-between;
  }
  .activity_box {
    margin-left: 20px;
    margin-bottom: 20px;
  }
}
</style>
